// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'dart:convert';
import 'dart:typed_data';

import 'package:client/common/conn.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:client/main.dart';

void main() {
  testWidgets('conn test', (WidgetTester tester) async {
    // Uint8List byteList = Uint8List.fromList([0, 1, 2, 3]);
    // Uint16List byteList = Uint16List.fromList([0]);
    // ByteData byteData = ByteData.sublistView(byteList);
    // print(byteList.buffer.asUint32List());

    // var value = byteData.getUint16(0, Endian.big);
    // print(value);

    // byteData.setInt16(0, 1024, Endian.big);
    // print(byteData.getUint16(0));

    ByteData b = ByteData(2);
    b.setUint16(0, 1024, Endian.big);

    List<int> bs = b.buffer.asUint8List();

    print(bs);

    BytesBuilder builder = BytesBuilder();
    // 动态构建字节数据
    builder.add(bs);
    // 获取构建的字节流
    List<int> bytes = builder.toBytes();
    print(bytes);

    // 0000 0100 0000 0000

    // 创建 ByteData 对象
    ByteData b1 = ByteData.sublistView(Uint8List.fromList(bs));

    // 从字节流中读取 16 位整数，按照大端字节序
    int v = b1.getInt16(0, Endian.big);

    // 输出读取的整数
    print(v); // 输出 4660 (0x1234)
  });

  testWidgets('msg test', (WidgetTester tester) async {
    var i = ConnMsg(100, "hello");
    var a = ByteMsg.msg2Byte(i);
    print(a);
    var b = ByteMsg.byte2Msg(a);
    print(b);
  });

  testWidgets('msg', (WidgetTester tester) async {
    List<int> data = [
      0,
      30,
      0,
      200,
      123,
      34,
      99,
      111,
      100,
      101,
      34,
      58,
      48,
      44,
      34,
      109,
      115,
      103,
      34,
      58,
      34,
      115,
      117,
      99,
      99,
      101,
      115,
      115,
      34,
      125,
      0,
      73,
      0,
      232,
      123,
      34,
      110,
      97,
      109,
      101,
      34,
      58,
      34,
      97,
      98,
      99,
      34,
      44,
      34,
      116,
      105,
      109,
      101,
      34,
      58,
      34,
      50,
      48,
      50,
      53,
      45,
      48,
      55,
      45,
      50,
      56,
      32,
      49,
      54,
      58,
      48,
      51,
      58,
      52,
      57,
      34,
      44,
      34,
      117,
      117,
      105,
      100,
      34,
      58,
      34,
      122,
      77,
      50,
      71,
      67,
      113,
      74,
      114,
      70,
      115,
      90,
      101,
      70,
      121,
      70,
      111,
      34,
      125
    ];

    print(data.length);

    //解析数据
    ByteData bLen = ByteData.sublistView(Uint8List.fromList(data), 0, 2);
    int len = bLen.getUint16(0, Endian.big);
    print("len-->");
    print(len);
    List<int> bodyData = data.sublist(2, len);
    print("bodyData-->");
    print(bodyData);
    ByteData bCmd = ByteData.sublistView(Uint8List.fromList(bodyData), 0, 2);
    int cmd = bCmd.getUint16(0, Endian.big);
    print("cmd-->");
    print(cmd);

    var info = utf8.decode(bodyData.sublist(2).toList(), allowMalformed: true);
    print("info-->");
    print(info);
  });

  testWidgets('msg test1', (WidgetTester tester) async {
    List<int> buff = [];
    buff.addAll([10, 20, 30, 40, 50, 60, 70, 80, 90]);
    print("buff-->");
    print(buff);
    buff.removeRange(0, 2);
    print("buff after remove-->");
    print(buff);
    buff.addAll([100, 110, 120]);
    print("buff after add-->");
    print(buff);
  });
}
